﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Threading;
using SimpleJson;
using Pomelo.DotNetClient;


namespace SimpleClient 
{
    public class Floor:GameObject
    {
/*
        private string _gate_server_ip = "127.0.0.1";
        private int _gate_server_port = 3010;
        public static JsonObject _users = null;
        public static PomeloClient _pomelo = null;
        */
        private System.Threading.Timer timer;//计时器
        
        private static int unit = 15; 
        private readonly int length = 28 * unit;
        private readonly int width = 20 * unit;

        private Connector connector;//管理服务器连接

        public bool alive;

        private Point dot; //运动场左上角位置
        public int score; //游戏分数

        private Player p; //玩家
        public static ArrayList pList = new ArrayList();//其他玩家
        public static ArrayList hList = new ArrayList(); //所有敌人
        

        public Player P
        {
            get { return p; }
        }
        public Floor(Point d) 
        {
            dot = d; //d赋给左上角位置字段
            Main.xMax = Main.xMin + length;
            Main.yMax = Main.yMin + width;
            alive = true;
        }
        public void Move(Graphics g, int position)
        {
            p.Move(g, position);
            //Display(g);
        }
        public void Init(Graphics g)
        {
            p = new Player(dot, 10);
            p.c = Color.Chocolate;
            p.Display(g);

            Random r = new Random();
            for(int i = 0; i < 3 ;i++)
            {
                int x = r.Next(Main.xMin, Main.xMax-15);
                int y = r.Next(Main.yMin, Main.yMax -15);
                Point pot = new Point(x,y);
                Home h = new Home(pot, 10);
                hList.Add(h);
            }
            for (int i = 0; i < 4; i++)
            {
                int x = r.Next(Main.xMin, Main.xMax - 15);
                int y = r.Next(Main.yMin, Main.yMax - 15);
                Point pot = new Point(x, y);
                Player pt = new Player(pot, 10);
                pList.Add(pt);
            }
            foreach (Home h in hList)
            {
                h.Display(g);
            }
            foreach (Player pt in pList)
            {
                pt.Display(g);
            }
        }

        public void Display(Graphics g) //显示运动场方法，参数为一图形对象
        {
            Pen pen = new Pen(Color.Red); //创建笔并用红色实例化它
            g.DrawRectangle(pen, dot.X, dot.Y, length, width); //用上笔画一位置在dot，长、宽分别为length和width的矩形
        }
        public void UnDisplay(Graphics g)
        {

        }
        public void UpDate(Graphics g)
        {
            Display(g);
            foreach (Home h in hList)
            {
                h.UpDate(g);
            }
            foreach (Player pt in pList)
            {
                pt.UpDate(g);
            }
            
           
            if(!p.alive)
            {
                p.UpDate(g);
               
                alive = false;
                //p.UnDisplay(g);
            }
            else
            {
                p.UpDate(g);
            }
        }

        public void connect()
        {
            connector = new Connector();
            connector.run();
            /*
            _pomelo = new PomeloClient(_gate_server_ip, _gate_server_port);
            _pomelo.connect(null,(data)=>
            {
                _pomelo.request("connector.entryHandler.entry", (data2) =>
                    {
                    });
            });
            _pomelo.on("test", (data) =>
            {
                Console.WriteLine("这就是一条测试消息");
            });
            */
            timer = new System.Threading.Timer(new TimerCallback(timer_Elapsed), null, 0, 50);
           
        }
        private void timer_Elapsed(object sender)
        {
            Console.Out.WriteLine(p.getPosition().ToString());
            JsonObject msg = new JsonObject();
            msg["content"] = p.getPosition().ToString();
            msg["rid"] = "3";
            msg["uid"] = "dante*3";
            msg["target"] = "*";
            msg["from"] = "dante";
            Main._pomelo.notify("chat.chatHandler.sendP", msg);
        }
    }
}